package com.androidcourse.shounews.SqlLite;

import static android.content.ContentValues.TAG;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.androidcourse.shounews.Entity.MyNews;

import java.util.ArrayList;
import java.util.List;

public class MyDBHelper extends SQLiteOpenHelper {
    public static final String DB_NAME="MyNews.db";
    public static final String TABLE_NAME="MyNews";
    public static final int DB_VERSION=1;
    private static MyDBHelper myDBHelper=null;
    private SQLiteDatabase read;
    private SQLiteDatabase write;
    private MyDBHelper(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
    public static MyDBHelper getInstance(Context context){
       if(myDBHelper==null) return new MyDBHelper(context);
       return myDBHelper;
    }
    //获取读
    public SQLiteDatabase getRead() {
        Log.d(TAG, "getRead: ");
        if(read==null||!read.isOpen())
        {
            read=this.getReadableDatabase();
        }
        return read;
    }
    //获取写
    public SQLiteDatabase getWrite() {
        if(write==null||!write.isOpen()){
            write = this.getWritableDatabase();
        }
        return write;
    }
    //关闭连接
    public void closeLink(){
        if(read!=null&& read.isOpen()){
            this.close();
        }
        if(write!=null&& write.isOpen()){
            this.close();
        }
    }
    //添加信息
    public boolean insert(MyNews myNews){
        ContentValues values =new ContentValues();
        values.put("name",myNews.getName());
        values.put("content_url",myNews.getLink());
        try{
            return write.insert(TABLE_NAME, null, values) >= 0;
        }catch (Exception e){
            return false;
        }
    }
    //查询全部
    public List<MyNews> queryAll(){
        List<MyNews> list=new ArrayList<>();
        Cursor cursor= read.query(TABLE_NAME,null,null,null,null,null,null);
        while (cursor.moveToNext()){
            MyNews myNews=new MyNews();
            myNews.setName(cursor.getString(0));
            myNews.setLink(cursor.getString(1));
            list.add(myNews);
        }
        return list;
    }
    //名字查询
    public List<MyNews> queryByName(String name){
        List<MyNews> list=new ArrayList<>();
        Cursor cursor= read.query(TABLE_NAME,null,"name=?",new String[]{name},null,null,null);
        while (cursor.moveToNext()){
            MyNews myNews=new MyNews();
            myNews.setName(cursor.getString(0));
            myNews.setLink(cursor.getString(1));
            list.add(myNews);
            list.add(myNews);
        }
        return list;
    }
    //名字删除
    public boolean deleteByName(String name){
        try{
            Log.d(TAG, "deleteByName: ");
            return write.delete(TABLE_NAME, "name=?", new String[]{name}) >= 0;
        }catch (Exception e)
        {
            Log.e(TAG, "deleteByName: ", e);
            return false;
        }
    }
    //删除全部
    public boolean deleteAll(){
        try{
            return write.delete(TABLE_NAME, "1=1", null) >= 0;
        }catch (Exception e) {
            return false;
        }

    }
    //名字更新
    public boolean updateByName(String name,MyNews myNews){
        ContentValues values =new ContentValues();
        values.put("name",myNews.getName());
        values.put("content_url",myNews.getLink());
        try{
            return write.update(TABLE_NAME, values, "name=?", new String[]{myNews.getName()}) >= 0;
        }catch (Exception e){
            return false;
        }
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(TAG, "onCreate: ");
        String sql="create table if not exists "+ TABLE_NAME+"(" +
                "name text not null," +
                "content_url text" +
                ")";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
